package mis.ds.ps.model.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl;

import mis.ds.ps.model.dao.ModulosDao;
import mis.ds.ps.model.obj.Docentes;
import mis.ds.ps.model.obj.Modulos;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
 
 
/**
 * DAO for table: Modulos.
 * @author autogenerated
 */
@Repository
public class ModulosDaoImpl extends GenericHibernateDaoImpl<Modulos, Long> implements ModulosDao {
	
	public static final int ID = 1;
	public static final int CODIGO = 3;
	public static final int NOME = 6;

	private static Logger logger = Logger.getLogger(ModulosDaoImpl.class);

	private Connection mysqlConn;
	
	/** Constructor method. */
	public ModulosDaoImpl() {
		super(Modulos.class);
	}
	
	public List<Modulos> getModulos() {
		
		List<Modulos> lstModulos = new ArrayList<Modulos>();
		
		String sql = "SELECT * from modulos";
		System.out.println(sql);

//		PreparedStatement prest;
		Statement stmt;
		try {
//			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
//			logger.debug("SQL select statement [" + prest.toString() + "]");
//			ResultSet rs = prest.executeQuery();
				
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/initial_timetable","root","12345");
			stmt = (Statement) con.createStatement();
			logger.debug("SQL select statement [" + stmt.toString() + "]");
			ResultSet rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				Modulos md = new Modulos(Long.valueOf(rs.getString(ID)));
				md.setCodigo(rs.getInt(CODIGO));
				md.setNome(rs.getString(NOME));	
				System.out.println(rs.getString(NOME));
				
				lstModulos.add(md);
			}
			System.out.println(lstModulos.size());
		} catch (SQLException e1) {
			System.out.println(e1.getMessage());
			logger.debug("Cannot get Docentes from DB ", e1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lstModulos;

	}

	public Connection getMysqlConn() {
		return mysqlConn;
	}
	
	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}
}

	
